<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 订单管理
 * @package app\erp\model
 */
class ErpGoodsOrder extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_goods_order';
	
	use SoftDelete;
    protected $deleteTime = 'order_status';
    protected $defaultSoftDelete = 0;
	
	 /**
     * 订单 - sku
     */
    public function getGoodsOrderSkulist(){
        return $this -> hasMany('app\erp\model\ErpGoodsOrderSkulist','order_id', 'id');
    }

    /**
     * 添加数据
     * @param ordercode 	string 订单号
     * @param pay_type 	    int 支付方式 1微信 2支付宝
     * @param uid 	        int    用户id
     * @param pay_status 	int    支付状态
     * @param pay_expiration_time 	int 过期时间
     * @param pay_orderid 	    varchar    支付订单号
     * @param pay_price 	    decimal    支付总价
     * @param stroe_id 	int    门店id
     * @param order_type 	int    订单类型 0是正常购买订单 1团购订单
     * @param ps_type 	int    提取状态
     * @author Naruto
     * @return array_out(code, message, (int)id)
     */
    public function addData($ordercode, $uid, $pay_type, $pay_orderid, $pay_price, $store_id, $ps_type, $pay_status =0, $order_type=0){
        $data['ordercode']                  = $ordercode;
        $data['uid']                        = $uid;
        $data['pay_type']                   = $pay_type;
        $data['pay_status']                 = $pay_status;
        $data['pay_expiration_time']        = time() + 60*30;
        $data['pay_orderid']                = $pay_orderid;
        $data['pay_price']                  = $pay_price;
        $data['store_id']                   = $store_id;
        $data['ps_type']                    = $ps_type;
        $data['order_type']                 = $order_type;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 删除数据
     * @param int $order_id 订单id
     * @author shiqiang
     * @return bool
     */
    public function deleteData($uid, $order_id){
        $map        = [];
        $map['uid']      = $uid;
        $map['id'] 		 = $order_id;
        $res = self::destroy(function($query) use ($map){
            $query->where($map);
        });
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
